//
//  MSTradeSuccessViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/15.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSTradeSuccessViewController.h"
#import "MSTradeSuccessTableVC.h"
#import "MSTradeCommentViewController.h"

@interface MSTradeSuccessViewController ()
@property (nonatomic, strong) NSString   *coinCount;
@property (nonatomic, strong) UILabel    *tradePriceLabel;
@property (nonatomic, strong) UILabel    *memberTypeLabel;
@property (nonatomic, strong) UILabel    *timeLabel;
@property (nonatomic, strong) UILabel    *payLabel;
@property (nonatomic, strong) UIView     *footerView;
@property (nonatomic, strong) GXButton              *commentBtn;
@property (nonatomic, strong) MSTradeSuccessTableVC *tableVC;
@end

@implementation MSTradeSuccessViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray* viewControllers = [HDUtil checkNilArray:[rootNavigation viewControllers]];
    NSInteger selfIndex = [viewControllers indexOfObject:self];
    if (selfIndex-1>=0) {
        [viewControllers[selfIndex-1] removeFromParentViewController];
    }
}

- (void)initNavigationBar{
    [super initNavigationBar];
    titleLable.text = @"交易成功";
    rightBarBtn.hidden = NO;
    [rightBarBtn setTitle:@"完成" forState:UIControlStateNormal];
}

- (void)rightBarBtnAction{
    [rootNavigation popViewControllerAnimated:YES];
}

- (void)initUI{
    self.tableVC = [[MSTradeSuccessTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    _coinCount = @"";
    [self layoutBottomView];
}

- (void)layoutBottomView{
    UIView *headView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headView.backgroundColor = [UIColor whiteColor];
    self.tableVC.tableView.tableHeaderView = headView;
    
    _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
    _footerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableFooterView = _footerView;
    
    UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    bgView.backgroundColor = [UIColor whiteColor];
    [_footerView addSubview:bgView];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 5, SCREEN_WIDTH-24, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [_footerView addSubview:horizonLineView];
    
    UIView *horizonLineView2 =[[UIView alloc] initWithFrame:CGRectMake(0, horizonLineView.bottom+38.4, SCREEN_WIDTH, 0.6)];
    horizonLineView2.backgroundColor = kNormalGray;
    [_footerView addSubview:horizonLineView2];
    
    self.tradePriceLabel = [self creatLabel:CGRectMake(12, horizonLineView.bottom+8, SCREEN_WIDTH/2, 20)];
    self.memberTypeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH/2-20, self.tradePriceLabel.top, 100, 20)];
    self.timeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH-92, self.tradePriceLabel.top, 80, 20)];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    self.payLabel = [self creatLabel:CGRectMake(18, 40, SCREEN_WIDTH-10, 68)];
    
    self.commentBtn = [[GXButton alloc] initWithFrame:CGRectMake(12, self.payLabel.bottom, SCREEN_WIDTH-24, 45)];
    self.commentBtn.titleLabel.font = HeitiFont(16);
    [self.commentBtn setTitle:@"去评价" forState:UIControlStateNormal];
    [self.commentBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [self.commentBtn setBackgroundColor:[UIColor whiteColor]];
    self.commentBtn.layer.borderColor = kBlueColor.CGColor;
    self.commentBtn.layer.borderWidth = 1;
    self.commentBtn.layer.cornerRadius = 2;
    [self.commentBtn addTarget:self action:@selector(commentClick) forControlEvents:UIControlEventTouchUpInside];
    [_footerView addSubview:self.commentBtn];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    [_footerView addSubview:label];
    return label;
}

- (void)setValueGiveLabelWithModel:(MSTradeModel *)model{
    NSMutableAttributedString *priceAttstr = [[NSMutableAttributedString alloc] initWithString:@"价格     x " attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.bounds = CGRectMake(0, -4, 17, 17);
    textAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [priceAttstr insertAttributedString:attachmentString atIndex:6];
    
    NSAttributedString *priStr = [[NSAttributedString alloc] initWithString:model.tradePrice attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kLightBlackColor}];
    [priceAttstr appendAttributedString:priStr];
    self.tradePriceLabel.attributedText = priceAttstr;
    
    NSString *memberTypeStr = [NSString stringWithFormat:@"[%@]",model.memberType];
    NSMutableAttributedString *memberAttstr = [[NSMutableAttributedString alloc] initWithString:memberTypeStr attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    if ([model.memberLevel integerValue] >0) {
        NSTextAttachment *memberAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        memberAttachment.bounds = CGRectMake(0, -2.5, 15, 12.5);
        memberAttachment.image = [UIImage imageNamed:@"diamond"];
        NSAttributedString *memberAttachmentStr = [NSAttributedString attributedStringWithAttachment:memberAttachment];
        
        for (int i = 0; i<[model.memberLevel integerValue]; i++) {
            [memberAttstr insertAttributedString:memberAttachmentStr atIndex:memberTypeStr.length+i];
        }
    }
    self.memberTypeLabel.attributedText = memberAttstr;
    
    NSMutableAttributedString *timeAttstr = [[NSMutableAttributedString alloc] initWithString:model.time attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
    textAttachment2.image = [UIImage imageNamed:@"time"];
    NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
    [timeAttstr insertAttributedString:attachmentString2 atIndex:0];
    
    self.timeLabel.attributedText = timeAttstr;
    
    NSString *price = [NSString stringWithFormat:@"您已支付： %@",model.tradePrice];
    
    NSMutableAttributedString *paidAttstr = [[NSMutableAttributedString alloc] initWithString:price attributes:@{NSFontAttributeName:HeitiFont(15),NSForegroundColorAttributeName:kLightBlackColor}];
    
    NSTextAttachment *paidAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    paidAttachment.bounds = CGRectMake(0, -5, 20, 20);
    paidAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *paidAttachmentString = [NSAttributedString attributedStringWithAttachment:paidAttachment];
    [paidAttstr insertAttributedString:paidAttachmentString atIndex:6];
    
    NSAttributedString *symbolStr = [[NSAttributedString alloc] initWithString:@" x " attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    [paidAttstr insertAttributedString:symbolStr atIndex:7];
    
    NSAttributedString *suffixStr = [[NSAttributedString alloc] initWithString:@"  剩余江湖币 12" attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    [paidAttstr appendAttributedString:suffixStr];
    
    self.payLabel.attributedText = paidAttstr;
}

- (void)testDataFunc{
    MSTradeModel *model = [[MSTradeModel alloc] init];
    model.title = @"上海浦东世和空港中心商业住房项目";
    model.projectType = @"求购业主联系人信息";
    model.name = @"周世鹏";
    model.company = @"上海世贸";
    model.position = @"经理";
    model.phone = @"13345346587";
    model.content = @"经理上海浦东世和空港中心商业住房项目上海浦东世和空港中心商业住房项目";
    model.tradePrice = @"12";
    model.memberType = @"[匿名会员]";
    model.memberLevel = @"2";
    model.time = @"2016.9.15";
    model.typeGroup = @"process";
    
    self.tableVC.model = _model;
    [self setValueGiveLabelWithModel:_model];
    [netEngine requestWalletWithTarget:self.VctrID hudView:self.contentView];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"Wallet"]!=nil){
            _coinCount = responseDic[@"Wallet"];
            [self setPrice];
        }
    }
}

- (void)setPrice{
    NSString *price = [NSString stringWithFormat:@"您已支付： %@",_model.tradePrice];
    
    NSMutableAttributedString *paidAttstr = [[NSMutableAttributedString alloc] initWithString:price attributes:@{NSFontAttributeName:HeitiFont(15),NSForegroundColorAttributeName:kLightBlackColor}];
    
    NSTextAttachment *paidAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    paidAttachment.bounds = CGRectMake(0, -5, 20, 20);
    paidAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *paidAttachmentString = [NSAttributedString attributedStringWithAttachment:paidAttachment];
    [paidAttstr insertAttributedString:paidAttachmentString atIndex:6];
    
    NSAttributedString *symbolStr = [[NSAttributedString alloc] initWithString:@" x " attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    [paidAttstr insertAttributedString:symbolStr atIndex:7];
    
    NSAttributedString *suffixStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  剩余江湖币 %@",_coinCount] attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    [paidAttstr appendAttributedString:suffixStr];
    
    self.payLabel.attributedText = paidAttstr;
}

- (void)commentClick{
    MSTradeCommentViewController *vc = [[MSTradeCommentViewController alloc] init];
    vc.orderId = _model.orderId;
    [rootNavigation pushViewController:vc animated:YES];
}

@end
